/**
 * 服装资源的二进制缓存
 * author:noodles
 * */
package com.gamebase.role
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	public class DesignByteCache
	{
		private var cacheDic:Dictionary;
		private static var _instance:DesignByteCache; 
		
		public function DesignByteCache()
		{
			cacheDic = new Dictionary();
		}
		
		private static function get instance():DesignByteCache
		{
			if(_instance == null)
			{
				_instance = new DesignByteCache();
			}
			return _instance;
		}
		
		/**以MovieClip方式获取服装资源
		 * @param cb:获取后的回调，回调必须带一个MovieClip参数，该参数是目标MovieClip的引用。
		 * */
		public static function getMovieClip(cacheName:String,cb:Function):void
		{
			if(hasNoCache(cacheName))
			{
				cb(null);
				return;
			}
			
			var mc:MovieClip;
			var load:Loader = new Loader();
			load.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
			load.loadBytes(instance.cacheDic[cacheName]);
			
			function swfLoaded(evt:Event):void
			{
				mc = load.content as MovieClip;
				cb(mc);
			}
		}
		
		
		public static function getCache(cacheName:String):ByteArray
		{
			return instance.cacheDic[cacheName] as ByteArray;
		}
		
		//将服装加入缓存
		public static function addToCache(mcData:ByteArray,designName:String):void
		{
			if(instance.cacheDic[designName]==null)
				instance.cacheDic[designName] = mcData;
		}
		
		//将服装从缓存中销毁
		public static function removeCache(mcData:ByteArray,designName:String):void
		{
			if(instance.cacheDic[designName])
				delete instance.cacheDic[designName];
		}
		
		public static function hasNoCache(designName:String):Boolean
		{
			if(designName==null || instance.cacheDic[designName]==null)
				return true;
			else
				return false;
		}

	}
}